package cn.edu.cqu.controllers;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import cn.edu.cqu.domains.AcademicNews;
import cn.edu.cqu.domains.CollegeNews;
import cn.edu.cqu.domains.CollegeProfile;
import cn.edu.cqu.domains.PartyNews;
import cn.edu.cqu.domains.StudyNews;
import cn.edu.cqu.domains.TeacherInfo;
import cn.edu.cqu.services.AcademicNewsCRUD;
import cn.edu.cqu.services.AdminCRUD;
import cn.edu.cqu.services.CollegeNewsCRUD;
import cn.edu.cqu.services.CollegeProfileCRUD;
import cn.edu.cqu.services.ContactCRUD;
import cn.edu.cqu.services.PartyNewsCRUD;
import cn.edu.cqu.services.StudyNewsCRUD;
import cn.edu.cqu.services.TeacherInfoCRUD;

@RestController
public class TempController
{
	@Autowired
	@Qualifier("AdminCRUDHibernate")
	private AdminCRUD adminCRUD;
	@Autowired
	@Qualifier("AcademicNewsCRUDHibernate")
	private AcademicNewsCRUD academicNewsCRUD ;
	@Autowired
	@Qualifier("CollegeNewsCRUDHibernate")
	private CollegeNewsCRUD collegeNewsCRUD;
	@Autowired
	@Qualifier("CollegeProfileCRUDHibernate")
	private CollegeProfileCRUD collegeProfileCRUD ;
	@Autowired
	@Qualifier("PartyNewsCRUDHibernate")
	private PartyNewsCRUD partyNewsCRUD ;
	@Autowired
	@Qualifier("ContactCRUDHibernate")
	private ContactCRUD contactCRUD;
	@Autowired
	@Qualifier("StudyNewsCRUDHibernate")
	private StudyNewsCRUD studyNewsCRUD;
	@Autowired
	@Qualifier("TeacherInfoCRUDHibernate")
	private TeacherInfoCRUD teacherInfoCRUD;

	@RequestMapping(value="/imgNews.json") 
	public HashMap<String, Object> imgNews() throws IOException
	{
		HashMap<String, Object> results = new HashMap<String, Object>();
		Document doc = Jsoup.connect("http://www.cs.cqu.edu.cn/").get();
		Elements link = doc.select("#u_u4_ifocus_piclist").first().select("ul").select("li");
		Elements link2 = doc.select("#u_u4_ifocus_tx_ul").first().select("li");
		LinkedList<String> urlData = new LinkedList<>();
		LinkedList<String> imgData = new LinkedList<>();
		LinkedList<String> titleData = new LinkedList<>();
		for (Element element : link)
		{
			String url = "http://www.cs.cqu.edu.cn/" + element.select("a").attr("href");
			String imgSrc =  "http://www.cs.cqu.edu.cn" +element.select("a").select("img").attr("src");
			urlData.addLast(url);
			imgData.addLast(imgSrc);
		}
		for (Element element : link2)
		{
			String title = element.text();
			titleData.addLast(title);
		}
		results.put("urlData", urlData);
		results.put("imgData", imgData);
		results.put("titleData", titleData);
		results.put("status", 0);
		return results;
	}

	@RequestMapping(value="/textNews.json") 
	public HashMap<String, Object> textNews() throws IOException
	{
		HashMap<String, Object> results = new HashMap<String, Object>();


		Document doc = Jsoup.connect("http://www.cs.cqu.edu.cn/jxgz/yjsjx.htm").get();
		Elements links = doc.select("ul").first().select("li");
		LinkedList<String> urlData = new LinkedList<>();
		LinkedList<String> titleData = new LinkedList<>();
		for (Element element : links)
		{
			String url = "http://www.cs.cqu.edu.cn/" + element.select("a").get(1).attr("href").substring(3);
			String title =  element.select("a").get(1).attr("title");
			urlData.addLast(url);
			titleData.addLast(title);
		}
		results.put("urlData", urlData);
		results.put("titleData", titleData);
		results.put("status", 0);
		return results;
	}

	@RequestMapping(value="/updateAllAcademicNews.json") 
	public void updateAllAcademicNews() throws ParseException, IOException
	{
		for (int i = 1;; i++) {
			Document doc = null;
			try {
				doc = Jsoup.connect("http://www.cs.cqu.edu.cn/xsdt/"+ i+ ".htm").get();
			} catch (IOException e) {
				try {
					doc = Jsoup.connect("http://www.cs.cqu.edu.cn/xsdt.htm").get();
					i = 0;
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			Elements links = doc.select("ul").first().select("li");
			for (Element element : links)
			{
				AcademicNews academicNews = new AcademicNews();
				String tmpUrl = element.select("a").get(1).attr("href");
				if (tmpUrl.contains("..")) {
					tmpUrl = tmpUrl.substring(3);
				}
				String url = "http://www.cs.cqu.edu.cn/" + tmpUrl;
				String title =  element.select("a").get(1).attr("title");
				academicNews.setaNewsID(url.substring(url.lastIndexOf("/")+1, url.lastIndexOf(".")));
				academicNews.setaNewsURL(url);
				academicNews.setaNewsTitle(title);

				Document newDoc = Jsoup.connect(url).get();
				Elements contentstyle171142 = newDoc.select(".contentstyle171142");
				String str = contentstyle171142.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
				str = str.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
				System.out.println(title);
				System.out.println("--------");
				Element tmp = contentstyle171142.select("img").first();
				String imgUrl = "";
				if (tmp !=null && !tmp.equals("")) {
					imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src");
				}
				Elements timestyle171142 = newDoc.select(".timestyle171142");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date d = sdf.parse(timestyle171142.text());
				academicNews.setaNewsImgURL(imgUrl);
				academicNews.setaNewsContent(str);
				academicNews.setaNewsDate(d);
				academicNewsCRUD.edit(academicNews);
			}
			if (i==0) {
				break;
			}
		}
	}

	@RequestMapping(value="/updateAllCollegeNews.json") 
	public void updateAllCollegeNews() throws ParseException, IOException
	{
		for (int i = 1;; i++) {
			Document doc = null;
			try {
				doc = Jsoup.connect("http://www.cs.cqu.edu.cn/xyxw/"+ i+ ".htm").get();
			} catch (IOException e) {
				try {
					doc = Jsoup.connect("http://www.cs.cqu.edu.cn/xyxw.htm").get();
					i = 0;
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			Elements links = doc.select("ul").first().select("li");
			for (Element element : links)
			{
				CollegeNews collegeNews = new CollegeNews();
				String tmpUrl = element.select("a").get(1).attr("href");
				if (tmpUrl.contains("..")) {
					tmpUrl = tmpUrl.substring(3);
				}
				String url = "http://www.cs.cqu.edu.cn/" + tmpUrl;
				String title =  element.select("a").get(1).attr("title");
				collegeNews.setcNewsURL(url);
				collegeNews.setcNewsTitle(title);
				String id = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf("."));
				collegeNews.setcNewsID(id);
				Document newDoc = Jsoup.connect(url).get();
				Elements contentstyle171142 = newDoc.select(".contentstyle171142");
				String str = contentstyle171142.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
				System.out.println(title);
				System.out.println("--------");
				Element tmp = contentstyle171142.select("img").first();
				String imgUrl = "";
				if (tmp !=null && !tmp.equals("")) {
					imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src");
				}
				Elements timestyle171142 = newDoc.select(".timestyle171142");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date d = sdf.parse(timestyle171142.text());
				collegeNews.setcNewsImgURL(imgUrl);
				collegeNews.setcNewsContent(str);
				collegeNews.setcNewsDate(d);
				collegeNewsCRUD.edit(collegeNews);
			}
			if (i==0) {
				break;
			}
		}
	}

	@RequestMapping(value="/updateAllCollegeProfile.json") 
	public void updateAllCollegeProfile() throws ParseException, IOException
	{
		String urlArray[] = {"xyjj","yzdh","jgsz"};
		String chsArray[]={"学院简介","院长的话","机构设置"};
		for (int i = 0;i < urlArray.length; i++) {
			CollegeProfile collegeProfile = new CollegeProfile();
			String url = "http://www.cs.cqu.edu.cn/xygk/"+ urlArray[i]+ ".htm";
			System.out.println(url);
			Document doc = Jsoup.connect(url).get();
			Element content = doc.select(".winstyle170774").get(0);
			Element tmp = content.select("img").first();
			String imgUrl = "";
			if (tmp !=null && !tmp.equals("")) {
				imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src");
			}
			String contentStr = content.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
			collegeProfile.setCpURL(url);
			collegeProfile.setCpImgURL(imgUrl);
			collegeProfile.setCpContent(contentStr);
			collegeProfile.setCpTitle(chsArray[i]);
			collegeProfileCRUD.edit(collegeProfile);
		}
		CollegeProfile collegeProfile = new CollegeProfile();
		String url = "http://www.cs.cqu.edu.cn/xygk/xrld.htm";
		Document doc = Jsoup.connect(url).get();
		Element main = doc.select("#main").get(2);//第三个
		String contentStr = main.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
		contentStr = contentStr.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
		Element tmp = main.select("img").first();
		String imgUrl = "";
		if (tmp !=null && !tmp.equals("")) {
			imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src").substring(2);
		}
		collegeProfile.setCpImgURL(imgUrl);
		collegeProfile.setCpContent(contentStr);
		collegeProfile.setCpTitle("现任领导");
		collegeProfile.setCpURL(url);
		collegeProfileCRUD.edit(collegeProfile);
	}

	@RequestMapping(value="/updateAllPartyNews.json") 
	public void updateAllPartyNews() throws ParseException, IOException
	{
		Document doc = Jsoup.connect("http://www.cs.cqu.edu.cn/djgz/djxw.htm").get();
		Elements links = doc.select("ul").first().select("li");
		for (Element element : links)
		{
			PartyNews partyNews = new PartyNews();
			String tmpUrl = element.select("a").get(1).attr("href");
			if (tmpUrl.contains("..")) {
				tmpUrl = tmpUrl.substring(3);
			}
			String url = "http://www.cs.cqu.edu.cn/" + tmpUrl;
			String title =  element.select("a").get(1).attr("title");
			partyNews.setpNewsURL(url);
			partyNews.setpNewsTitle(title);
			String id = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf("."));
			partyNews.setpNewsID(id);
			Document newDoc = Jsoup.connect(url).get();
			Elements contentstyle171142 = newDoc.select(".contentstyle171142");
			System.out.println(title);
			System.out.println("--------");
			Element tmp = contentstyle171142.select("img").first();
			String imgUrl = "";
			if (tmp !=null) {
				imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src");
			}
			String str = contentstyle171142.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
			str = str.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
			Elements timestyle171142 = newDoc.select(".timestyle171142");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date d = sdf.parse(timestyle171142.text());
			partyNews.setpNewsImgURL(imgUrl);
			partyNews.setpNewsContent(str);
			partyNews.setpNewsDate(d);
			partyNewsCRUD.edit(partyNews);
		}
	}

	@RequestMapping(value="/updateAllStudyNews.json") 
	public void updateAllStudyNews() throws ParseException, IOException
	{
		String urlArray[] = {"bkjx","yjsjx","xsgz"};
		String chsArray[]={"本科教学","研究生教学","学生工作"};
		/*选定类别URL*/
		for (int i = 0;i < urlArray.length; i++) {
			/*选定第几页URL*/
			for (int j = 1; j < chsArray.length; j++) {
				Document doc = null;
				try {
					doc = Jsoup.connect("http://www.cs.cqu.edu.cn/jxgz/"+urlArray[i]+"/" +j+ ".htm").get();
				} catch (IOException e) {
					try {
						doc = Jsoup.connect("http://www.cs.cqu.edu.cn/jxgz/"+urlArray[i]+".html").get();
						j= 0;
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				Elements links = doc.select("ul").first().select("li");
				for (Element element : links)
				{
					StudyNews studyNews = new StudyNews();
					String tmpUrl = element.select("a").get(1).attr("href");
					if (tmpUrl.contains("..")) {
						if (j==0) {/*../*/
							tmpUrl = tmpUrl.substring(3);
						}
						else{/*../../*/
							tmpUrl = tmpUrl.substring(6);
						}
					}
					String url = "http://www.cs.cqu.edu.cn/" + tmpUrl;
					String title =  element.select("a").get(1).attr("title");
					studyNews.setsNewsURL(url);
					studyNews.setsNewsTitle(title);
					String id = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf("."));
					studyNews.setsNewsID(id);
					Document newDoc = Jsoup.connect(url).get();
					Elements contentstyle171142 = newDoc.select(".contentstyle171142");
					String str = contentstyle171142.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
					str = str.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
					System.out.println(title);
					System.out.println("--------");
					Element tmp = contentstyle171142.select("img").first();
					String imgUrl = "";
					if (tmp !=null && !tmp.equals("")) {
						imgUrl = "http://www.cs.cqu.edu.cn" + tmp.attr("src");
					}
					Elements timestyle171142 = newDoc.select(".timestyle171142");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					Date d = sdf.parse(timestyle171142.text());
					studyNews.setsNewsType(i);
					studyNews.setsNewsImgURL(imgUrl);
					studyNews.setsNewsContent(str);
					studyNews.setsNewsDate(d);
					studyNewsCRUD.edit(studyNews);
				}
				/*退出条件*/
				if (j == 0) {
					break;
				}
			}

		}
	}

	@RequestMapping(value="/updateAllTeacherInfo.json") 
	public void updateAllTeacherInfo() throws ParseException, IOException
	{
		String urlArray[] = {"jsjkxx","jsjgcx","xxjsx","jcx","bgs","sys"};
		String chsArray[]={"计算机科学系","计算机工程系","信息技术系","基础系","办公室","实验室"};
		/*选定类别URL*/
		for (int i = 0;i < urlArray.length; i++) {
			/** HtmlUnit请求web页面 */
			WebClient wc = new WebClient(BrowserVersion.CHROME);
			wc.getOptions().setUseInsecureSSL(true);
			wc.getOptions().setJavaScriptEnabled(true); // 启用JS解释器，默认为true
			wc.getOptions().setCssEnabled(false); // 禁用css支持
			wc.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时，是否抛出异常
			wc.getOptions().setTimeout(100000); // 设置连接超时时间 ，这里是10S。如果为0，则无限期等待
			wc.getOptions().setDoNotTrackEnabled(false);
			HtmlPage page = wc.getPage("http://www.cs.cqu.edu.cn/szdw/jzyg/"+ urlArray[i]+ ".htm");
			Document doc=Jsoup.parse(page.asXml());
			Element u_u3_imgcontent = doc.getElementById("u_u3_imgcontent");
			Elements spans = u_u3_imgcontent.select("span a");
			String depatment = chsArray[i];
			for (Element element : spans)
			{
				TeacherInfo teacherInfo = new TeacherInfo();
				String newUrl = element.attr("href");
				String name = element.select("span.titlestyle171851").text();
				String imgId = "u_u3_img"+newUrl.substring(newUrl.lastIndexOf("/")+1, newUrl.lastIndexOf(".")); 
				Element img = u_u3_imgcontent.getElementById(imgId);
				String imgUrl = img.attr("src");
				Document newDoc = Jsoup.connect(newUrl).get();
				Element main = newDoc.select("#vsb_content").get(0);
				String str = main.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
				str = str.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
				teacherInfo.settContent(str);
				teacherInfo.settDepatment(depatment);
				teacherInfo.settImg(imgUrl);
				teacherInfo.settName(name);
				teacherInfo.settUrl(newUrl);
				teacherInfoCRUD.edit(teacherInfo);
			}
			wc.close();
		}
	}

	public static void main(String[] args) throws IOException
	{
//		Document doc = Jsoup.connect("http://www.cs.cqu.edu.cn/info/1139/1262.htm").get();
//		Element main = doc.select("#main").get(2);
//		System.out.println(main.html());
		
		
//		/** HtmlUnit请求web页面 */
//		WebClient wc = new WebClient(BrowserVersion.CHROME);
//		wc.getOptions().setUseInsecureSSL(true);
//		wc.getOptions().setJavaScriptEnabled(true); // 启用JS解释器，默认为true
//		wc.getOptions().setCssEnabled(false); // 禁用css支持
//		wc.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时，是否抛出异常
//		wc.getOptions().setTimeout(100000); // 设置连接超时时间 ，这里是10S。如果为0，则无限期等待
//		wc.getOptions().setDoNotTrackEnabled(false);
//		HtmlPage page = wc.getPage("http://www.cs.cqu.edu.cn/szdw/jzyg/jsjgcx.htm");
//		Document doc=Jsoup.parse(page.asXml());
//		Element u_u3_imgcontent = doc.getElementById("u_u3_imgcontent");
//		Elements spans = u_u3_imgcontent.select("span a");
//		String depatment = "计算机科学系";
//		for (Element element : spans)
//		{
//			TeacherInfo teacherInfo = new TeacherInfo();
//			String newUrl = element.attr("href");
//			String name = element.select("span.titlestyle171851").text();
//			String imgId = "u_u3_img"+newUrl.substring(newUrl.lastIndexOf("/")+1, newUrl.lastIndexOf(".")); 
//			Element img = u_u3_imgcontent.getElementById(imgId);
//			String imgUrl = img.attr("src");
//			Document newDoc = Jsoup.connect(newUrl).get();
//			Element main = newDoc.select("#vsb_content").get(0);
//			String str = main.html().replace("src=\"../", "src=\"http://www.cs.cqu.edu.cn/").replace("src=\"/", "src=\"http://www.cs.cqu.edu.cn/");
//			str = str.replace("href=\"../", "href=\"http://www.cs.cqu.edu.cn/").replace("href=\"/", "href=\"http://www.cs.cqu.edu.cn/");
//			
//		}
//		wc.close();
		
	}
}
